package com.mz.ozy.activity.home;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mz.ozy.R;
import com.mz.ozy.adapter.CityAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.DbBean;
import com.mz.ozy.utils.AddrDbManager;
import com.mz.ozy.widget.CustomGridView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 城市选择
 */
public class CitySelectActivity extends BaseActivity {
    @BindView(R.id.searchEt)
    EditText mSearchEt;
    @BindView(R.id.cityRecyclerView)
    RecyclerView mCityRecyclerView;
    private List<DbBean> searchList = new ArrayList<>();
    private List<DbBean> cityList = new ArrayList<>();

    private CityAdapter mCityAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_select);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("选择区域");
        tvBack.setImageResource(R.mipmap.delete_icon);
        tvCenterTitle.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        tvTitle.setVisibility(View.GONE);
//        EventBus.getDefault().register(this);
        initData();

    }

    private void initData() {
        cityList.addAll(AddrDbManager.cbeans);
        mCityAdapter = new CityAdapter(this, cityList);
        mCityRecyclerView.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mCityRecyclerView.setAdapter(mCityAdapter);
        mSearchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                searchList.clear();
                if (!TextUtils.isEmpty(mSearchEt.getText().toString().trim())) {
                    for (DbBean bean : cityList) {
                        if (bean.getAreaName().contains(s.toString())) {
                            searchList.add(bean);
                        }
                        mCityAdapter.setCityList(searchList);
                        mCityAdapter.notifyDataSetChanged();
                    }
                } else {
                    mCityAdapter.setCityList(cityList);
                    mCityAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    public void goNearbyBsFragment(DbBean mDbBean) {
        Intent intent = new Intent();
        intent.putExtra("dbbean", mDbBean);
        setResult(101, intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);
    }

}
